﻿using Device.API.Applcation.Command.CarriersCommand;
using Device.Domain;
using Device.Infrstructre.Interface;
using MediatR;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Device.API.Applcation.CommandHandlers.CarriersCommandHandlers
{
    /// <summary>
    /// 修改承运商中介者
    /// </summary>
    public class UpdCarriersModelCommandHandler : IRequestHandler<UpdCarriersModelCommand, int>
    {
        private readonly ICarrierRepository _carrierRepository;
        //构造函数
        public UpdCarriersModelCommandHandler(ICarrierRepository carrierRepository)
        {
            _carrierRepository = carrierRepository;
        }

        public async Task<int> Handle(UpdCarriersModelCommand request, CancellationToken cancellationToken)
        {
            //判断名称是否重复
            var res = await _carrierRepository.GetAsync(x => x.IsDel == false && x.CarriersName == request.CarriersName && x.Office_Id==request.Office_Id && x.User_Id==request.User_Id);
            if (res.Count() > 0)
            {
                return -1;//用户名已存在
            }
            var carrierModel = new CarriersModel() { CarriersId = request.CarriersId, CarriersCode = request.CarriersCode, CarriersName = request.CarriersName, Describe = request.Describe, Office_Id = request.Office_Id, User_Id = request.User_Id, AddName = request.AddName, AddTime = request.AddTime, UpdName = request.UpdName, UpdTime = request.UpdTime };
            return await _carrierRepository.UpdateAsync(carrierModel);
        }
    }
}
